import java.util.Random;
import java.util.Scanner;

/**
 * 加减法口算练习系统 - V0.2
 * 生成50道100以内的加减法算式，每行显示多个算式
 */
class Calculation_System_V0_2 {
    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("===== 加减法口算练习系统 - V0.2 =====");
        System.out.println("本次将生成50道加减法算式，每行显示多个算式！\n");
        
        // 定义每行显示的算式数量
        int equationsPerLine = 2;
        int totalEquations = 50;
        
        // 创建存储算式和答案的数组
        String[] equations = new String[totalEquations];
        int[] answers = new int[totalEquations];
        
        // 生成50道算式
        for (int i = 0; i < totalEquations; i++) {
            boolean isAddition = random.nextBoolean();
            int num1, num2, answer;
            
            if (isAddition) {
                // 加法：确保和不超过100
                num1 = random.nextInt(99) + 1; // 1-99
                num2 = random.nextInt(100 - num1) + 1; // 确保num1+num2 <= 100
                answer = num1 + num2;
            } else {
                // 减法：确保差不小于0
                num2 = random.nextInt(99) + 1; // 1-99
                num1 = random.nextInt(100 - num2) + num2; // 确保num1 >= num2
                answer = num1 - num2;
            }
            
            // 构建算式字符串
            String operator = isAddition ? " + " : " - ";
            equations[i] = (i + 1) + ". " + num1 + operator + num2 + " = " + answer;
            answers[i] = answer;
        }
        
        // 每行显示多个算式，保持整齐
        for (int i = 0; i < totalEquations; i += equationsPerLine) {
            StringBuilder line = new StringBuilder();
            
            for (int j = 0; j < equationsPerLine && i + j < totalEquations; j++) {
                // 格式化每个算式，确保宽度一致
                line.append(String.format("%-35s", equations[i + j]));
            }
            
            System.out.println(line.toString());
        }
        
        System.out.println("\n===== 算式生成完成 =====");
        System.out.println("共生成" + totalEquations + "道加减法算式。");
        System.out.println("每行显示" + equationsPerLine + "个算式，方便查看和练习。");
        System.out.println("\n实现说明：");
        System.out.println("1. 加法算式：确保两个数的和不超过100");
        System.out.println("2. 减法算式：确保被减数大于或等于减数，差不小于0");
        System.out.println("3. 每行显示多个算式，并保持格式整齐");
        System.out.println("4. 每个算式都包含题目序号和正确答案");
        
        scanner.close();
    }
}